<?php
namespace haibao\cms\business\attendance;
use \haibao\cms\data\attendance\AttendanceRequest as AttendanceRequestData;
use \haibao\cms\model\filter\attendance\AttendanceRequest as AttendanceRequestFilter;
use \haibao\cms\business\BusinessException;
use haibao\frame\data\query\Condition;
Class AttendanceRequest extends \haibao\cms\business\Base{
	
	public static function addAttendanceRequest($model){
		$data = new AttendanceRequestData();
		$data->add($model);
	}
	
	public static function getAttendanceRequest($filter,$isOne = true){
		$data = new AttendanceRequestData();
		if($isOne){
			return $data->getOne($filter);
		}else{
			return $data->getAll($filter);
		}
	}
	
	public static function getAttendanceRequestList($filter){
	    $data = new AttendanceRequestData();
	    return $data->getAll($filter);
	}
	
	public static function checkRequestOverTimeIsExist($requestModel){
		if(!$requestModel->JobNum){
			throw new BusinessException('员工工号不能为空',self::DATA_EMPTY_ID);
		}
		if(!$requestModel->StartDate){
			throw new BusinessException('加班时间不能为空',self::DATA_EMPTY);
		}
		
		$data = new AttendanceRequestData();
		if($data->checkRequestOverTimeIsExist($requestModel)){
			return true;
		}else{
			return false;
		}
	}
	
	public static function getNotApprovedUser($startDate,$endDate){
		$data = new AttendanceRequestData();
		return $data->getNotApprovedUser($startDate, $endDate);
	}
	
	public static function updateRequestByid($id,$arrSet){
		if(empty($id)){
			throw new BusinessException('id不能为空',self::DATA_EMPTY_ID);
		}
		if(empty($arrSet)){
			throw new BusinessException('字段不能为空',self::DATA_EMPTY);
		}
		$data = new AttendanceRequestData();
		$data->updateById($id, $arrSet);
	}
	
	public static function removeRequestById($id){
		if(!$id){
			throw new BusinessException('申请ID不能为空',self::DATA_EMPTY_ID);
		}
		$data = new AttendanceRequestData();
		$data->removeById((int)$id);
	}
	
	public static function getRequestType(){
		return array(
			\haibao\cms\model\data\attendance\AttendanceRequest::TYPE_STAFF=>'休假申请',
			\haibao\cms\model\data\attendance\AttendanceRequest::TYPE_OVERTIME=>'加班申请',
			\haibao\cms\model\data\attendance\AttendanceRequest::TYPE_CLICK_RECORD=>'打卡申请',
		);
	}
	
	public static function getRequestStatus(){
		return array(
				\haibao\cms\model\data\attendance\AttendanceRequest::STATUS_NOT=>'未审核',
				\haibao\cms\model\data\attendance\AttendanceRequest::STATUS_AGREE=>'已通过',
				\haibao\cms\model\data\attendance\AttendanceRequest::STATUS_BACK=>'已驳回',
				\haibao\cms\model\data\attendance\AttendanceRequest::STATUS_ING=>'审核中',
				\haibao\cms\model\data\attendance\AttendanceRequest::STATUS_INVALID=>'已过期',
		);
	}
	
	/**
	 * 判断员工休假申请是否已经存在(婚假/陪产假等)
	 */
	public static function checkLeaveAttendanceIsRequest($requestModel){
		
		$filter = new AttendanceRequestFilter();
		$filter->where(AttendanceRequestFilter::CONDITION_FIELD_NAME_JOBNUM,Condition::CONDITION_EQUAL,$requestModel->JobNum);
		$filter->where(AttendanceRequestFilter::CONDITION_FIELD_NAME_REQUEST_STATUS,Condition::CONDITION_NOTEQUAL,\haibao\cms\model\data\attendance\AttendanceRequest::STATUS_BACK);
		
		$filter->where(AttendanceRequestFilter::CONDITION_FIELD_NAME_REQUEST_TYPE,Condition::CONDITION_EQUAL,$requestModel->RequestType);
		$filter->where(AttendanceRequestFilter::CONDITION_FIELD_NAME_REQUEST_NUMBER,Condition::CONDITION_EQUAL,(int)$requestModel->RequestNumber);

		if($requestModel->RequestNumber == \haibao\cms\model\data\attendance\AttendanceDefine::MATERNITY_CHECK_LEAVE){

			$firstday = date('Y-m-01',strtotime($requestModel->StartDate));
			$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
			$filter->where(AttendanceRequestFilter::CONDITION_FIELD_NAME_START_DATE,Condition::CONDITION_GREATEREQUAL,$firstday);
			$filter->where(AttendanceRequestFilter::CONDITION_FIELD_NAME_END_DATE,Condition::CONDITION_LESSEQUAL,$lastday);
		}
		$data = new AttendanceRequestData();
		if($data->getAll($filter)){
			return true;
		}
		return false;
	
	}
	
	/**
	 * 更新当前考勤周期的考勤全部失效
	 */
	public static function updateAttendaceRequestToInvalid(){
		
		$filter = new AttendanceRequestFilter();
		$filter->where(AttendanceRequestFilter::CONDITION_FIELD_NAME_REQUEST_STATUS,Condition::CONDITION_IN,array(\haibao\cms\model\data\attendance\AttendanceRequest::STATUS_NOT,\haibao\cms\model\data\attendance\AttendanceRequest::STATUS_ING));

		$startDate = date('Y-m-d',mktime(0, 0, 0,date('m',strtotime('-1 month')) , 16, date("Y")));
		$endDate = date('Y-m-d',mktime(0, 0, 0,date('m') , 15, date("Y")));
		$filter->where(AttendanceRequestFilter::CONDITION_FIELD_NAME_START_DATE,Condition::CONDITION_GREATEREQUAL,$startDate);
		$filter->where(AttendanceRequestFilter::CONDITION_FIELD_NAME_END_DATE,Condition::CONDITION_LESSEQUAL,$endDate);
		
		$data = new AttendanceRequestData();
		$data->update(array(AttendanceRequestFilter::CONDITION_FIELD_NAME_REQUEST_STATUS=>\haibao\cms\model\data\attendance\AttendanceRequest::STATUS_INVALID), $filter);
		
	}
	
	public static function getRequestById($id){

		if(empty($id)){
			throw new BusinessException('id不能为空',self::DATA_EMPTY_ID);
		}
		$data = new AttendanceRequestData();
		return $data->getOneById($id);
	}
}